


package ciicos.check.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.check.entity.CheckItemsEntity;
import ciicos.check.model.checksitems.CheckItemsForm;
import ciicos.check.model.checksitems.CheckItemsInfoVO;
import ciicos.check.model.checksitems.CheckItemsListVO;
import ciicos.check.model.checksitems.CheckItemsPagination;
import ciicos.check.service.CheckItemsService;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * 巡检项
 *
 * @版本： V3.2.0
 * @版权：
 * @作者： 产研项目租
 * @日期： 2023-08-16
 */
@Slf4j
@RestController
@Api(tags = "巡检项", value = "checkManage")
@RequestMapping("/api/checkManage/ChecksItems")
public class CheckItemsController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private CheckItemsService assetsChecksService;


    /**
     * 列表
     *
     * @param assetsChecksPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody CheckItemsPagination assetsChecksPagination) throws IOException {
        //树形判断是否有参数，有参数不造树
        List<CheckItemsEntity> list = assetsChecksService.getList(assetsChecksPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<CheckItemsListVO> listVO = JsonUtil.getJsonToList(list, CheckItemsListVO.class);
        for (CheckItemsListVO assetsChecksVO : listVO) {
            assetsChecksVO.setName_name(assetsChecksVO.getName());

            assetsChecksVO.setStandard_name(assetsChecksVO.getStandard());

            assetsChecksVO.setWay_name(generaterSwapUtil.getDicName(assetsChecksVO.getWay(), "544538088265756677"));

            assetsChecksVO.setUnit_name(generaterSwapUtil.getDicName(assetsChecksVO.getUnit(), "544538225188810757"));

            assetsChecksVO.setMax_name(assetsChecksVO.getMax());

            assetsChecksVO.setMin_name(assetsChecksVO.getMin());

            assetsChecksVO.setType_name(generaterSwapUtil.getDicName(assetsChecksVO.getType(), "544538159636033541"));

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(assetsChecksPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param assetsChecksForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid CheckItemsForm assetsChecksForm) throws DataException {
        String b = assetsChecksService.checkForm(assetsChecksForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        CheckItemsEntity entity = JsonUtil.getJsonToBean(assetsChecksForm, CheckItemsEntity.class);
        entity.setId(mainId);
        assetsChecksService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<CheckItemsInfoVO> info(@PathVariable("id") String id) {
        CheckItemsEntity entity = assetsChecksService.getInfo(id);
        CheckItemsInfoVO vo = JsonUtil.getJsonToBean(entity, CheckItemsInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<CheckItemsInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        CheckItemsEntity entity = assetsChecksService.getInfo(id);
        CheckItemsInfoVO vo = JsonUtil.getJsonToBean(entity, CheckItemsInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setName(vo.getName());

        vo.setStandard(vo.getStandard());

        vo.setWay(generaterSwapUtil.getDicName(vo.getWay(), "450994478518370309"));

        vo.setUnit(generaterSwapUtil.getDicName(vo.getUnit(), "451016419769843781"));

        vo.setMax(vo.getMax());

        vo.setMin(vo.getMin());

        vo.setType(generaterSwapUtil.getDicName(vo.getType(), "461457129509752261"));


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param assetsChecksForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid CheckItemsForm assetsChecksForm) throws DataException {
        String b = assetsChecksService.checkForm(assetsChecksForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo = userProvider.get();
        CheckItemsEntity entity = assetsChecksService.getInfo(id);
        if (entity != null) {
            CheckItemsEntity subentity = JsonUtil.getJsonToBean(assetsChecksForm, CheckItemsEntity.class);
            boolean b1 = assetsChecksService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        CheckItemsEntity entity = assetsChecksService.getInfo(id);
        if (entity != null) {
            assetsChecksService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }


}
